/* 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package smack.client.gui;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.Sasl;
import javax.security.sasl.SaslClient;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.util.Base64;

class AuthenticationThread implements Runnable, CallbackHandler {

    private final AuthenticationCallback authenticationCallback;

    private boolean isRunning;

    private boolean isAuthenticated;

    private String user;

    private String key;
    
    private final XMPPConnection connection ;

    AuthenticationThread(final XMPPConnection connection, final AuthenticationCallback authenticationCallback) {
        this.connection = connection;
        this.authenticationCallback = authenticationCallback;
        isRunning = true;
    }

    @Override
    public void run() { 
    	
    }

    void stop() {
        isRunning = false;
    }

    @Override
    public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for (final Callback callback : callbacks) {
            if (callback instanceof NameCallback) {
                NameCallback nameCallback = (NameCallback) callback;
                nameCallback.setName(user);

            } else if (callback instanceof PasswordCallback) {
                PasswordCallback passwordCallback = (PasswordCallback) callback;
                passwordCallback.setPassword(key.toCharArray());
            }
        }
    }

    void setUser(final String user) {
        this.user = user;
    }

    void setKey(final String key) {
        this.key = key;
    }
    
    interface AuthenticationCallback {

        void onAuthenticationSuccess();

        void onAuthenticationFailed();

        void onInbound(String message);

        void onOutbound(String message);

    }
}
